
// OpenCV Sample Application: facedetect.c

// Include header files
#include <windows.h>
#include <iostream>
using namespace std;
#include "cv.h"
#include "highgui.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>

#include "CDetector.h"

// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{
	// create detector
	CDetector *detector = new CDetector();
	detector->createWindows();

    // Find if the capture is loaded successfully or not.
	if (!detector->capture)
	{
		cout << "ERROR: Camera not initiated!" << endl;
		return 0;
	}

	// just grab a frame so we got something to start on
	detector->grabNewFrame();
	detector->fixFrame();
	detector->copyFrame();

	bool done = false;
	while(!done)
    {
		// store this frame in the old_frame surface, so we can use it later for motion detection
		cvCopy( detector->frame, detector->old_frame, 0 );
		if (!detector->grabNewFrame())
			break;

		detector->fixFrame();
		detector->copyFrame();
		if ((detector->mState == STATE_NOFACE) || (detector->mState == STATE_GOTFACE))
		{
			detector->findFace( detector->frame );
			detector->extractFaceColor( detector->HSVMODE ? detector->hsv_image : detector->frame );
			cout << "face color="<<detector->avgH<<","<<detector->avgS<<","<<detector->avgV << endl;
		}
		detector->produceMask( detector->HSVMODE ? detector->hsv_image : detector->frame );
		if (detector->mState == STATE_GOTFACE)
			detector->searchForHand(detector->hsv_mask);
		if (detector->mState == STATE_MAYBEHAND)
		{
			detector->searchForWave(detector->hsv_mask);
			cvRectangle( detector->frame_copy, cvPoint(detector->handRect.x,detector->handRect.y), cvPoint(detector->handRect.x+detector->handRect.width,detector->handRect.y+detector->handRect.height), CV_RGB(0,255,0), 2, 8, 0 );
			detector->moveMouse();
		}

		detector->drawFaceFrame( detector->frame_copy );
		detector->showMask();
		detector->showCam();

        // Wait for a while before proceeding to the next frame
        if( cvWaitKey( 10 ) >= 0 )
            break;
    }

	// free the detector
	detector->destroyWindows();
	delete detector;

    // return 0 to indicate successfull execution of the program
    return 0;
}



/*
			// produce minus-image
			cv::Mat mat1, mat2, mat3;
			mat1 = frame_copy;
			mat2 = old_frame;
			mat3 = end_result;
			cv::absdiff( mat1, mat2, mat3);

			// calc the sum of all pixel colors
			double sumLeft = sumPixels(&mat3,&(cv::Rect(0, 0, 320, 480)));
			double sumRight = sumPixels(&mat3,&(cv::Rect(320, 0, 320, 480)));

			// move the cursor
			POINT pkt;
			GetCursorPos(&pkt);
			if (sumLeft > 1000000)
				SetCursorPos(pkt.x-20, pkt.y);
			GetCursorPos(&pkt);
			if (sumRight > 1000000)
				SetCursorPos(pkt.x+20, pkt.y);
*/
